package com.ljb.commlibrary.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期相关的工具类
 * Created by ljb on 2017/6/26.
 */

public class DateUtils {
    public interface Format {
        String YMDHMS = "yyyy-MM-dd HH:mm:ss";
        String YMD = "yyyy-MM-dd";
        String YM = "yyyy-MM";
        String MD = "MM-dd";
        String D = "dd";
        String HMS = "HH:mm:ss";
        String HM = "HH:mm";
        String MS = "mm:ss";
    }

    /**
     * 计算两个日期之间的相差的天数
     *
     * @param startDay
     * @param endDay
     * @return
     */
    public static int calculateDaysFromTimestamp(String startDay, String endDay, String format) {
        long start = DateUtils.date2TimeStamp(startDay, format);
        long end = DateUtils.date2TimeStamp(endDay, format);
        int days = (int) ((end - start) / (60 * 60 * 24));
        L.e("start:" + start + ",end:" + end + ",days:" + days);
        return days;
    }

    /**
     * 获取当前系统时间戳,随系统时间的变化而变化
     *
     * @return 时间戳
     */
    public static long getTimeStamp() {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * 获取指定格式的日期
     *
     * @param format yyyy-MM-dd HH:mm:ss 年-月-日 时:分:秒
     * @return
     */
    public static String getCurrentDate(String format) {
        return timeStamp2Date(getTimeStamp(), format);
    }

    /**
     * 获取今天几日之前的日期
     *
     * @param pastDay 过去几日
     * @param format  想要获取到的指定日期格式
     * @return
     */
    public static String getPastDayDate(int pastDay, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - pastDay);
        Date pastDate = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(pastDate);
    }

    /**
     * 获取本月之前几个月的日期
     *
     * @param pastMonth 过去几个月
     * @param format    获取到的指定日期格式
     * @return
     */
    public static String getPastMonthDate(int pastMonth, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - pastMonth);
        Date pastDate = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(pastDate);
    }

    /**
     * 日期字符串转时间戳
     *
     * @param date   日期字符串
     * @param format yyyy-MM-dd HH:mm:ss 年-月-日 时:分:秒
     * @return 时间戳
     */
    public static long date2TimeStamp(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date).getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 时间戳转日期字符串
     *
     * @param timestamp 时间戳
     * @param format    yyyy-MM-dd HH:mm:ss 年-月-日 时:分:秒
     * @return 日期字符串
     */
    public static String timeStamp2Date(long timestamp, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(timestamp * 1000L));
    }

    public static long timeStamp2TimeStamp(long rawTimestamp, String format) {
        String date = timeStamp2Date(rawTimestamp, format);
        long timeStamp = date2TimeStamp(date, format);
        return timeStamp;
    }

    /**
     * 日期字符串转换为指定格式的日期字符串
     * @param date
     * @param format
     * @return
     */
    public static String date2Date(String date,String format){
        long timeStamp = date2TimeStamp(date, format);
        return timeStamp2Date(timeStamp,format);
    }

    /**
     * 获取网络时间戳,请在子线程调用
     *
     * @return 当前网络时间戳,-1表示获取失败
     */
    public static long getURLTime() {
        try {
            URL url = new URL("http://www.baidu.com");
            URLConnection uc = url.openConnection();
            uc.connect();
            return uc.getDate();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
